package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.dto.BasePowermeterVo;
import com.workcell.workdata.xthings.entity.BasePowermeter;
import com.workcell.workdata.xthings.mapper.BasePowermeterMapper;
import com.workcell.workdata.xthings.service.BasePowermeterService;
import org.springframework.stereotype.Service;

/**
 * 功率计型号
 *
 * @author 廖博文
 * @date 2022-11-28 10:16:00
 */
@Service
public class BasePowermeterServiceImpl extends ServiceImpl<BasePowermeterMapper, BasePowermeter> implements BasePowermeterService {

	/**
	 * 模糊查询分页
	 * @param basePowermeterVo
	 * @return
	 */
	@Override
	public Page<BasePowermeter> getBasePowermeterVaguePage(BasePowermeterVo basePowermeterVo) {
		LambdaQueryWrapper<BasePowermeter> basePowermeterCondition = new QueryWrapper<BasePowermeter>().lambda()
				.and(ObjectUtil.isNotEmpty(basePowermeterVo.getKeyWord()), i -> i
						.like(BasePowermeter::getPmName,basePowermeterVo.getKeyWord())
						.or()
						.like(BasePowermeter::getManufacturer,basePowermeterVo.getKeyWord()))
				.eq(ObjectUtil.isNotEmpty(basePowermeterVo.getState()),BasePowermeter::getState,basePowermeterVo.getState())
				.and(ObjectUtil.isNotEmpty(basePowermeterVo.getStartTime()) || ObjectUtil.isNotEmpty(basePowermeterVo.getEndTime())
						, i -> i.between(BasePowermeter::getCreateTime,basePowermeterVo.getStartTime(),basePowermeterVo.getEndTime()));
		Page<BasePowermeter> page = new Page<>(basePowermeterVo.getPageNum(), basePowermeterVo.getPageSize());
		page.addOrder(OrderItem.desc("create_time"));
		return this.page(page,basePowermeterCondition);
	}

	/**
	 * 修改功率计型号信息：此方法不能修改状态
	 * @param basePowermeter
	 * @return
	 */
	@Override
	public Boolean updateBasePowermeter(BasePowermeter basePowermeter) {
		Assert.isTrue(ObjectUtil.isNotEmpty(basePowermeter.getPmId()),"ID不能为空");
		basePowermeter.setState(null);
		// 查询该流量计的状态
		BasePowermeter basePowermeterOld = this.getById(basePowermeter.getPmId());
		Assert.isTrue(ObjectUtil.isNotEmpty(basePowermeterOld),"无该条记录，请核实您的操作");
		Assert.isTrue(HardwareStateEnum.DRAF.getCode().equals(basePowermeterOld.getState()),"状态为草稿才能修改");
		return this.updateById(basePowermeter);
	}

	/**
	 * 修改功率计型号信息状态
	 * @param basePowermeter
	 * @return
	 */
	@Override
	public Boolean updateStateById(BasePowermeter basePowermeter) {
		Assert.isTrue(ObjectUtil.isNotEmpty(basePowermeter.getPmId()),"ID不能为空");
		Assert.isTrue(ObjectUtil.isNotEmpty(basePowermeter.getState()),"状态不能为空");
		// 查询该流量计的状态
		BasePowermeter basePowermeterOld = this.getById(basePowermeter.getPmId());
		Assert.isTrue(ObjectUtil.isNotEmpty(basePowermeterOld),"无该条记录，请核实您的操作");
		if (HardwareStateEnum.INUSE.getCode().equals(basePowermeterOld.getState())
				|| HardwareStateEnum.SUSPEND.getCode().equals(basePowermeterOld.getState())){
			if (HardwareStateEnum.DRAF.getCode().equals(basePowermeter.getState())){
				Assert.isTrue(false,"当状态为使用中或暂停中时，不能修改为草稿状态");
			}
		}
		// 重新创建对象，以防前端误传其他参数
		BasePowermeter basePowermeterParam = new BasePowermeter();
		basePowermeterParam.setPmId(basePowermeter.getPmId());
		basePowermeterParam.setState(basePowermeter.getState());
		return this.updateById(basePowermeterParam);
	}

	/**
	 * 删除功率计型号信息
	 * @param pmId
	 * @return
	 */
	@Override
	public Boolean delBasePowermeterById(String pmId) {
		Assert.isTrue(ObjectUtil.isNotEmpty(pmId),"ID不能为空");
		// 查询该流量计的状态
		BasePowermeter basePowermeterOld = this.getById(pmId);
		Assert.isTrue(ObjectUtil.isNotEmpty(basePowermeterOld),"无该条记录，请核实您的操作");
		if (HardwareStateEnum.INUSE.getCode().equals(basePowermeterOld.getState())
				|| HardwareStateEnum.SUSPEND.getCode().equals(basePowermeterOld.getState())){
			Assert.isTrue(false,"当状态为使用中或暂停中时，不能删除该条记录");
		}
		// 删除记录
		return this.removeById(pmId);
	}
}
